﻿#include "TucsonMage.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    TucsonMage::TucsonMage(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool TucsonMage::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 3 || y > 3)
        {
            return false;
        }

        return (x <= 3 && y <= 3) || (x == y || x % 3 == y % 3);
    }

    void TucsonMage::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        bool range = !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!range && getEnvir()->Random->Next(3) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);
            WideLineAttack();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        ShockTime = 0;
    }

    void TucsonMage::WideLineAttack()
    {
        int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
        if (damage == 0)
        {
            return;
        }

        auto forward = Functions::PointMove(getCurrentLocation(), getDirection(), 1);

        Cell *cell = getCurrentMap()->GetCell(forward);
        if (!cell->Objects.empty())
        {
            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                {
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }

                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {ob, damage, DefenceType::ACAgility});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                }
                else
                {
                    continue;
                }

                break;
            }
        }

        auto direction = Functions::PreviousDir(getDirection());

        for (int d = 0; d < 3; d++)
        {
            for (int i = 1; i <= 2; i++)
            {
                Point *target = Functions::PointMove(forward, direction, i);

                if (!getCurrentMap()->ValidPoint(target))
                {
                    continue;
                }

                cell = getCurrentMap()->GetCell(target);
                if (cell->Objects.empty())
                {
                    continue;
                }

                for (int o = 0; o < cell->Objects.size(); o++)
                {
                    MapObject *ob = cell->Objects[o];
                    if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                    {
                        if (!ob->IsAttackTarget(this))
                        {
                            continue;
                        }

                        int delay = Functions::MaxDistance(getCurrentLocation(), ob->getCurrentLocation()) * 50 + 500; //50 MS per Step
                        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + delay, {ob, damage, DefenceType::ACAgility});
                        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                    }
                    else
                    {
                        continue;
                    }

                    break;
                }
            }

            direction = Functions::NextDir(direction);
        }
    }
}
